home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bisonpcb.zip / OUTPUT.C < prev    next >
Text File  |  1987-02-13  |  25KB  |  1,269 lines

  1. /* Output the generated parsing program for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.   
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.   
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.   
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* functions to output parsing data to various files.  Entries are:
  22.   
  23.   output_headers ()
  24.   
  25. Output constant strings to the beginning of certain files.
  26.   
  27.   output_trailers()
  28.   
  29. Output constant strings to the ends of certain files.
  30.   
  31.   output ()
  32.   
  33. Output the parsing tables and the parser code to ftable.
  34.   
  35. The parser tables consist of:  (starred ones needed only for the semantic parser)
  36.   
  37. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  38.   
  39. yytname = vector of string-names indexed by bison token number
  40.   
  41. yyrline = vector of line-numbers of all rules.  For yydebug printouts.
  42.   
  43. * yyrhs = vector of items of all rules.
  44.         This is exactly what ritems contains.
  45.   
  46. * yyprhs[r] = index in yyrhs of first item for rule r.
  47.   
  48. yyr1[r] = symbol number of symbol that rule r derives.
  49.   
  50. yyr2[r] = number of symbols composing right hand side of rule r.
  51.   
  52. * yystos[s] = the symbol number of the symbol that leads to state s.
  53.   
  54. yydefact[s] = default rule to reduce with in state s,
  55.           when yytable doesn't specify something else to do.
  56.           Zero means the default is an error.
  57.   
  58. yydefgoto[i] = default state to go to after a reduction of a rule that
  59.            generates variable ntokens + i, except when yytable
  60.            specifies something else to do.
  61.   
  62. yypact[s] = index in yytable of the portion describing state s.
  63.             The lookahed token's type is used to index that portion
  64.             to find out what to do.
  65.   
  66.         If the value in yytable is positive,
  67.         we shift the token and go to that state.
  68.   
  69.         If the value is negative, it is minus a rule number to reduce by.
  70.   
  71.         If the value is zero, the default action from yydefact[s] is used.
  72.   
  73. yypgoto[i] = the index in yytable of the portion describing 
  74.              what to do after reducing a rule that derives variable i + ntokens.
  75.              This portion is indexed by the parser state number
  76.          as of before the text for this nonterminal was read.
  77.          The value from yytable is the state to go to.
  78.   
  79. yytable = a vector filled with portions for different uses,
  80.           found via yypact and yypgoto.
  81.   
  82. yycheck = a vector indexed in parallel with yytable.
  83.       It indicates, in a roundabout way, the bounds of the
  84.       portion you are trying to examine.
  85.   
  86.       Suppose that the portion of yytable starts at index p
  87.       and the index to be examined within the portion is i.
  88.       Then if yycheck[p+i] != i, i is outside the bounds
  89.       of what is actually allocated, and the default
  90.       (from yydefact or yydefgoto) should be used.
  91.       Otherwise, yytable[p+i] should be used.
  92.   
  93. YYFINAL = the state number of the termination state.
  94. YYFLAG = most negative short int.  Used to flag ??
  95. YYNTBASE = ntokens.
  96.   
  97. */
  98.  
  99. /*
  100.  * Port to PC by Whit Gregg
  101.  *               Nourse, Gregg & Browne, Inc.
  102.  *         1 Horizon Road
  103.  *         Fort Lee, NJ  07024
  104.  */
  105.  
  106.  
  107. #include <stdio.h>
  108. #include <malloc.h>
  109. #include "machine.h"
  110. #include "new.h"
  111. #include "files.h"
  112. #include "gram.h"
  113. #include "state.h"
  114. #include "func.h"
  115.  
  116. #define    MAXTABLE 8080
  117.  
  118.  
  119. extern char **tags;
  120. extern int tokensetsize;
  121. extern int final_state;
  122. extern core **state_table;
  123. extern shifts **shift_table;
  124. extern errs **err_table;
  125. extern reductions **reduction_table;
  126. extern short *accessing_symbol;
  127. extern unsigned *LA;
  128. extern short *LAruleno;
  129. extern short *lookaheads;
  130. extern char *consistent;
  131. extern short *goto_map;
  132. extern short *from_state;
  133. extern short *to_state;
  134.  
  135.  
  136. static int nvectors;
  137. static int nentries;
  138. static short **froms;
  139. static short **tos;
  140. static short *tally;
  141. static short *width;
  142. static short *actrow;
  143. static short *state_count;
  144. static short *order;
  145. static short *base;
  146. static short *pos;
  147. static short *table;
  148. static short *check;
  149. static int lowzero;
  150. static int high;
  151.  
  152.  
  153.  
  154.  
  155. void
  156. output_headers()
  157. {                /* WG */
  158.     if (semantic_parser) {
  159.         fprintf(fguard, "\n#include %c%s%c\n", '"', attrsfile, '"');    /* WG */
  160.         fprintf(fguard, "extern int yyerror;\n");    /* WG */
  161.         fprintf(fguard, "extern int yycost;\n");    /* WG */
  162.         fprintf(fguard, "extern char * yymsg;\n");    /* WG */
  163.         fprintf(fguard, "extern YYSTYPE yyval;\n\n");    /* WG */
  164.         fprintf(fguard, "yyguard(n, yyvsp, yylsp)\n");    /* WG */
  165.         fprintf(fguard, "register int n;\n");    /* WG */
  166.         fprintf(fguard, "register YYSTYPE *yyvsp;\n");    /* WG */
  167.         fprintf(fguard, "register YYLTYPE *yylsp;\n");    /* WG */
  168.         fprintf(fguard, "{\n  yyerror = 0;\nyycost = 0;\n  yymsg = 0;\n");    /* WG */
  169.         fprintf(fguard, "switch (n)\n    {");    /* WG */
  170.  
  171. /*        fprintf(faction, "\n#include %c%s%c\n", '"', attrsfile, '"'); */
  172. /*        fprintf(faction, "extern YYSTYPE yyval;\n"); */
  173. /*        fprintf(faction, "extern int yychar;\n"); */
  174.         fprintf(faction, "yyaction(n, yyvsp, yylsp)\n");    /* WG */
  175.         fprintf(faction, "register int n;\n");    /* WG */
  176.         fprintf(faction, "register YYSTYPE *yyvsp;\n");    /* WG */
  177.         fprintf(faction, "register YYLTYPE *yylsp;\n");    /* WG */
  178.         fprintf(faction, "\t{\n\tswitch (n)\n\t\t{");    /* WG */
  179.         }
  180.     else {
  181.         fprintf(faction, "\n  switch (yyn) {\n");    /* WG */
  182.         }
  183.     }
  184.  
  185. void
  186. output_trailers()
  187. {                /* WG */
  188.     if (semantic_parser) {
  189.         fprintf(fguard, "\n    }\n}\n");
  190.         fprintf(faction, "\n    }\n}\n");
  191.         }
  192.     else
  193.         fprintf(faction, "\n}\n");
  194.     }
  195.  
  196.  
  197. void
  198. output()
  199. {                /* WG */
  200.     int c;
  201.  
  202.     /*
  203.      * output_token_defines(ftable);    /* JF put out token defines
  204.      * FIRST 
  205.      */
  206.     if (!semantic_parser) {    /* JF Put out other stuff */
  207.         rewind(fattrs);
  208.         while ((c = getc(fattrs)) != EOF)
  209.             putc((char) c, ftable);    /* WG */
  210.         }
  211.     /* output_program();    /* JF do it NOW */
  212.     if (semantic_parser)
  213.         fprintf(ftable, "#include \"%s\"\n", attrsfile);
  214.     fprintf(ftable, "#include <stdio.h>\n\n");
  215.  
  216.     free_itemsets();
  217.     output_defines();
  218.     output_token_translations();
  219.     if (semantic_parser)
  220.         output_gram();
  221.     FREE(ritem);
  222.     if (semantic_parser)
  223.         output_stos();
  224.     output_rule_data();
  225.     output_actions();
  226.     output_parser();
  227.     output_program();
  228.     }
  229.  
  230. void
  231. output_token_translations()
  232. {                /* WG */
  233.     register int i, j;
  234.  
  235. /*   register short *sp; JF unused */
  236.  
  237.     if (translations) {
  238.         fprintf(ftable, "\n#define YYTRANSLATE(x) (yytranslate[x])\n");
  239.  
  240.         if (ntokens < 127)    /* play it very safe; check maximum
  241.                      * element value.  */
  242.             fprintf(ftable, "\nstatic char yytranslate[] = {     0");
  243.         else
  244.             fprintf(ftable, "\nstatic short yytranslate[] = {     0");
  245.  
  246.         j = 10;
  247.         for (i = 1; i <= max_user_token_number; i++) {
  248.             putc(',', ftable);
  249.  
  250.             if (j >= 10) {
  251.                 putc('\n', ftable);
  252.                 j = 1;
  253.                 }
  254.             else {
  255.                 j++;
  256.                 }
  257.  
  258.             fprintf(ftable, "%6d", token_translations[i]);
  259.             }
  260.  
  261.         fprintf(ftable, "\n};\n");
  262.         }
  263.     else {
  264.         fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
  265.         }
  266.     }
  267.  
  268.  
  269.  
  270. void
  271. output_gram()
  272. {                /* WG */
  273.     register int i;
  274.     register int j;
  275.     register short *sp;
  276.  
  277.     fprintf(ftable, "\nstatic short yyprhs[] = {     0");
  278.  
  279.     j = 10;
  280.     for (i = 1; i <= nrules; i++) {
  281.         putc(',', ftable);
  282.  
  283.         if (j >= 10) {
  284.             putc('\n', ftable);
  285.             j = 1;
  286.             }
  287.         else {
  288.             j++;
  289.             }
  290.  
  291.